package zhihuilan.common.utils;


import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.QueryOrderByIdRequest;


import com.wechat.pay.java.service.payments.model.Transaction;
import org.apache.commons.lang3.time.DateUtils;
import zhihuilan.config.WeChatPayMiniConfig;
import zhihuilan.entity.MyPayOrder;
import zhihuilan.service.MyPayOrderService;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

public class WechatPayUtils {

        /**
         * 支付
         * */
        public  static  String jsapiPay(MyPayOrder myPayOrder) {
                WeChatPayMiniConfig weChatPayMiniConfig = new WeChatPayMiniConfig();
                Date currentDate = new Date();
                try {
                        weChatPayMiniConfig.init();

                        JsapiService service = new JsapiService.Builder().config(   weChatPayMiniConfig.getConfig()).build();

                        // request.setXxx(val)设置所需参数，具体参数可见Request定义
                        PrepayRequest request = new PrepayRequest();
                        request.setAppid(weChatPayMiniConfig.getAppId());
                        request.setMchid(weChatPayMiniConfig.getMchId());
                        request.setNotifyUrl(weChatPayMiniConfig.getNotifyUrl());
                        Amount amount = new Amount();
                        amount.setTotal(myPayOrder.getAmount().multiply(new BigDecimal("100")).intValue());
                        request.setAmount(amount);
                        request.setDescription(myPayOrder.getTitle());
                        request.setOutTradeNo(myPayOrder.getOrder());
                        Calendar calendar = DateUtils.toCalendar(currentDate);
                        calendar.add(Calendar.MINUTE,WeChatPayMiniConfig.ORDER_PAY_TIME_OUT);
                        request.setTimeExpire(DateUtils.parseDate("yyyy-MM-dd'T'HH:mm:ss+08:00",calendar.getTime().toString()).toString());
                        // 调用下单方法，得到应答
                       return  service.prepay(request).getPrepayId();
                }catch (Exception e){
                        return e.toString();
                }
        }

        /**
         * 查询订单
         * */
        public  static String QueryOrderByIdRequest(MyPayOrder myPayOrder) {
                WeChatPayMiniConfig weChatPayMiniConfig = new WeChatPayMiniConfig();
                QueryOrderByIdRequest queryRequest = new QueryOrderByIdRequest();
                queryRequest.setMchid(weChatPayMiniConfig.getMchId());
                queryRequest.setTransactionId(myPayOrder.getTransactionid());
                JsapiService service = new JsapiService.Builder().config(weChatPayMiniConfig.getConfig()).build();
                try {
                        Transaction result = service.queryOrderById(queryRequest);
                        System.out.println(result.getTradeState());
                        return result.getTradeState().toString();
                } catch (ServiceException e) {
                        // API返回失败, 例如ORDER_NOT_EXISTS
                        System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
                        System.out.printf("reponse body=[%s]\n", e.getResponseBody());
                        return e.getErrorMessage();
                }
        }



}
